package bag;
//创建对象的方法  new 或者克隆接口Cloneable
class Money implements Cloneable{
    double a = 13.5;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}
class Person implements Cloneable{//这个接口是个空接口  作用就是表示这个类是可以克隆的

    public int age;
    public Money money = new Money();
    public void eat(){
        System.out.println("吃");
    }
    public Object clone() throws CloneNotSupportedException {//此时必须要重写克隆方法 比较特殊
        //return super.clone();//在这里需要看拷贝方法 才知道是浅拷贝还是深拷贝
        Person h = (Person) super.clone();
        h.money = (Money)this.money.clone();//这里就将引用也深拷贝了
        return h;
    }

    @Override
    public String toString() {
        return "Person{" +
                " age=" + age +
                '}';
    }
}
public class Main {
    public static void main(String[] args) throws CloneNotSupportedException {
        Person person = new Person();
        Person person1 = (Person) person.clone();//clone 类型是object的  要实现clone方法其类一定要实现克隆接口
        System.out.println(person1.age);
        person1.age = 20;
        System.out.println(person);
        System.out.println(person1);
        System.out.println("=======");
        Person person2 = (Person)person.clone();
        System.out.println(person.money.a);
        System.out.println(person2.money.a);
        System.out.println("=====");
        person2.money.a = 85.5;
        System.out.println(person.money.a);//这里就是浅拷贝
        System.out.println(person2.money.a);//改变之后就是深拷贝了
    }
}
